package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.bean.UserComDTO;
import com.xieyingwangluo.yxpart.entity.UserCommentModel;
import com.xieyingwangluo.yxpart.service.UserComService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wangchaonan
 * @Date: 2018/10/8 9:48
 * @Version 1.0
 */
@Component
public class UserComLogic {
    @Autowired
    private UserComService userComService;

    public Result add(UserComDTO userComDTO) {

        UserCommentModel userCommentModel = new UserCommentModel();
        BeanUtils.copyProperties(userComDTO, userCommentModel);
        if (userCommentModel != null) {
            int cnt = userComService.add(userCommentModel);
            if (cnt > 0) {
                return Result.generate(0, "Add success", userCommentModel);
            }
        }
        return Result.generate(1, "Add fail", null);
    }

    public Result getById(String noid) {
        UserCommentModel userCommentModel = userComService.getById(noid);
        if (userCommentModel != null) {
            return Result.generate(0, "Select success", userCommentModel);
        }
        return Result.generate(1, "Select fail", null);
    }

    public Result upd(String noid, UserComDTO userComDTO) {
        UserCommentModel userCommentModel = new UserCommentModel();
        BeanUtils.copyProperties(userComDTO, userCommentModel);
        userCommentModel.setNoid(noid);
        int cnt = userComService.upd(userCommentModel);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    public Result selA(UserComDTO userComDTO) {
        UserCommentModel userCommentModel = new UserCommentModel();
        BeanUtils.copyProperties(userComDTO, userCommentModel);
        List<UserCommentModel> list = userComService.selA(userCommentModel);
        System.out.println(list);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);
        }
        return Result.generate(1, "Select fail", null);
    }

    public Result del(String noid) {
        if (noid != null || !noid.equals("")){
            int cnt=userComService.del(noid);
            if(cnt>0){
                return Result.generate(0, "Del success", cnt);
            }
        }
        return Result.generate(1, "Del fail", null);
    }
}
